﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASKM.Rachao.Model;
using ASKM.Rachao.Model.Facade;
using ASKM.Rachao.Model.Dto;
using System.Web.Routing;
using System.Web.Security;

namespace ASKM.Rachao.Web.Site.Controllers
{
    public class HomeController : Controller
    {



        public ActionResult Index()
        {
            ViewBag.Message = "Rachão - O seu ponto de encontro nos Esportes";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }


        [HttpPost()]
        public ActionResult RegisterUser(InitialRegisterDto dto)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var facade = new Profile())
                    {                        
                        var playerProfileId = facade.InitialRegister(dto);

                        FormsAuthentication.SetAuthCookie(dto.Email, true);

                        RouteValueDictionary rvd = new RouteValueDictionary();
                        rvd.Add("id", playerProfileId);
                        return RedirectToAction("Details", "PlayerProfile", rvd);
                    }

                    
                }
                                             
            }
            catch(Rachao.Model.Exceptions.RachaoBusinessException rbex)
            {
                foreach (var item in rbex.ErrorMessages)
	            {
		            ModelState.AddModelError("RachaoBusinessException", item);
	            }                
            }
            
            return View("Index");       
            
        }
    }
}
